package com.jcroke.PC32.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.regex.Pattern;

/**
 * 
 * @author jcroke
 *
 */

public class FileUtil {

	public static ArrayList<File> getFilesInDirectory(final File directory,
			final boolean doRecursive) {
		return getFilesInDirectory(directory, doRecursive, "");
	}
	
	public static ArrayList<File> getFilesInDirectory(final File directory,
			final boolean doRecursive, final String patternFileFilter) {

		ArrayList<File> output = new ArrayList<File>();
		boolean includeFile = false;

		for (File fileEntry : directory.listFiles()) {

			if (fileEntry.isDirectory()) {
				if (doRecursive) {
					output.addAll(getFilesInDirectory(fileEntry, doRecursive,
							patternFileFilter));
				}
			}

			else {
				if (patternFileFilter == null
						|| patternFileFilter.length() == 0) {
					includeFile = true;
				}
				// Otherwise we need to filter by pattern
				else {
					includeFile = Pattern.matches(patternFileFilter,
							fileEntry.getName());
				}

				if (includeFile) {
					output.add(fileEntry);
				}
			}
		}
		return output;
	}
}
